package com.bdqn.ch04;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.ch04.entity.User;
import com.bdqn.ch04.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;
import java.util.zip.CheckedOutputStream;

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserMapperTester {
    @Resource
    private UserMapper userMapper;

    @Test
    public void testSelectById() {// 主键查询
        User user = userMapper.selectById(48);
        System.out.println("usrName：" + user.getUsrName());
    }

    @Test
    public void testAddUser() {
        //有特殊情况下 hash冲突
        System.out.println(userMapper.hashCode());
        User user = new User();
        // user.setUsrId(100L);
        user.setUsrName("测试名字");
        int count = userMapper.insert(user);
        System.out.println(count);
    }

    @Test
    public void testFind() {// 条件查询：根据用户名和密码查询
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("usr_name", "test");
        wrapper.eq("usr_password", "123456");
        List<User> list = userMapper.selectList(wrapper);
        if (list != null) {
            for (User user : list) {
                System.out.println("usrId：" + user.getUsrId());
            }
        }
    }

    @Test
    public void testPagination() {// 分页查询
       // QueryWrapper<User> wrapper = new QueryWrapper<User>();
        //wrapper.eq("usr_role_id", 1L);//查询条件：角色 Id 为 2
        int size = 5;
        int currentPage = 7;
        Page<User> page = new Page<User>(currentPage,size);
        IPage<User> userPager = userMapper.selectPage(page,null);
        List<User> userList = userPager.getRecords();
    }

}
